package com.atguigu.lock;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * list集合线程不安全
 */
public class ThreadDemo4 {
    public String temp = "11";
@Test
public void test(){
    HashMap<String, String> v0 = new HashMap<>();
    HashSet<String> v1 = new HashSet<>();
    v1.add("");

}
    public static void main(String[] args) {
        //创建ArrayList集合
//        List<String> list = new ArrayList<>();
        // Vector解决
        //   List<String> list = new Vector<>();

        //Collections解决
        // List<String> list = Collections.synchronizedList(new ArrayList<>());

        // CopyOnWriteArrayList解决
        //       List<String> list = new CopyOnWriteArrayList<>();   //写时复制技术，那个数组被violate修饰了
//        for (int i = 0; i <30; i++) {
//            new Thread(()->{
//                //向集合添加内容
//                list.add(UUID.randomUUID().toString().substring(0,8));
//                //从集合获取内容
//                System.out.println(list);
//            },String.valueOf(i)).start();
//        }

        //演示Hashset
//        Set<String> set = new HashSet<>();

        Set<String> set = new CopyOnWriteArraySet<>();
//        for (int i = 0; i <30; i++) {
//            new Thread(()->{
//                //向集合添加内容
//                set.add(UUID.randomUUID().toString().substring(0,8));
//                //从集合获取内容
//                System.out.println(set);
//            },String.valueOf(i)).start();
//        }

        //演示HashMap
//        Map<String,String> map = new HashMap<>();

        Map<String, String> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 30; i++) {
            String key = String.valueOf(i);
            new Thread(() -> {
                //向集合添加内容
                map.put(key, UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
    }
}
